package com.syf.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.UpdateById;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.syf.cmd.CompanyApplyCmd;
import com.syf.domain.store.BsSoftwareRelease;
import com.syf.mapper.BsSoftwareReleaseMapper;
import com.syf.service.BsSoftwareReleaseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syf.domain.store.BsCompanyApply;
import com.syf.mapper.BsCompanyApplyMapper;
import com.syf.service.BsCompanyApplyService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
*
*
*@author syf
*@create 2022-02-07 19:09
*/
@Slf4j
@Service
public class BsCompanyApplyServiceImpl extends ServiceImpl<BsCompanyApplyMapper, BsCompanyApply> implements BsCompanyApplyService{

    @Autowired
    private BsSoftwareReleaseService bsSoftwareReleaseService;  //软件

    /**
     * 分页条件查询--软件厂商申请列表
     * @return
     */
    @Override
    public Page<BsCompanyApply> findByPage(CompanyApplyCmd companyApplyCmd) {
        Integer applyStatus = companyApplyCmd.getApplyStatus();
        String companyName = companyApplyCmd.getCompanyName();
        Integer status = companyApplyCmd.getStatus();
        String companyCode = companyApplyCmd.getCompanyCode();
        Page<BsCompanyApply> page = new Page<>(companyApplyCmd.getCurrent(),companyApplyCmd.getSize());

        LambdaQueryWrapper<BsCompanyApply> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(companyName),BsCompanyApply::getCompanyName,companyName)
                .eq(applyStatus != null, BsCompanyApply::getApplyStatus,applyStatus)
                .eq(status != null, BsCompanyApply::getStatus,status)
                .like(StringUtils.isNotEmpty(companyCode), BsCompanyApply::getCompanyCode,companyCode)
                .orderByDesc(BsCompanyApply::getCreateTime);

        return page(page, wrapper);
    }

    /**
     * 修改软件厂商状态
     *
     * @param bsCompanyApply
     * @return
     */
    @Override
    @Transactional
    public boolean updateStatus(BsCompanyApply bsCompanyApply) {
        boolean update = updateById(bsCompanyApply);
        if(update){  //禁用厂商
            List<BsSoftwareRelease> releasesList = bsSoftwareReleaseService.selectSoftwareByCompanyId(bsCompanyApply.getId());
            if(!CollectionUtils.isEmpty(releasesList)){

                for (BsSoftwareRelease release:releasesList){
                    if(bsCompanyApply.getStatus() == 1){
                        release.setStatus(1); //软件下线
                    }else {
                        release.setStatus(0); //软件回复正常
                    }
                    bsSoftwareReleaseService.updateById(release);
                }

            }else {
                log.info("厂商信息修改成功，该厂商下还没有应用");
            }
        }
        return true;
    }

    /**
     * 企业是否已经存在
     *
     * @param companyName
     */
    @Override
    public boolean checkExist(String companyName) {
        LambdaQueryWrapper<BsCompanyApply> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BsCompanyApply::getCompanyName,companyName)
                .eq(BsCompanyApply::getApplyStatus,1);  //已通过
        BsCompanyApply apply = getOne(wrapper);
        if(ObjectUtils.isEmpty(apply)){
            return false;
        }
        return true;
    }

    @Override
    public BsCompanyApply selByCode(String companyCode) {
        LambdaQueryWrapper<BsCompanyApply> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(companyCode),BsCompanyApply::getCompanyCode,companyCode);
        List<BsCompanyApply> list = list(wrapper);
        if(CollectionUtil.isNotEmpty(list)){
            return list.get(0);
        }
        return null;

    }
}
